package com.ruipeng.shengyapo.utils;

import java.util.HashMap;
import java.util.Random;

import org.apache.log4j.chainsaw.Main;

import com.cloopen.rest.sdk.CCPRestSDK;

/**
 * 发送短信验证
 * @author zhengchb
 */
public class SendValidataCode {
	
	private final static String SMS_CODE="180286";
	
	private final static String SEND_URL = "app.cloopen.com";

	private final static String SEND_PORT = "8883";

	private final static String ACCOUNT_ID = "8a216da85c34774c015c6191d9520cf1";

	private final static String AUTH_TOKEN = "0789748e0ffe4363a224a79b1d91a026";

	private final static String APP_ID = "8aaf07085c346c5a015c619d69bc0d32";
	/*

	private final static String ACCOUNT_ID = "8a48b551506fd26f015089640e9b47d0";

	private final static String APP_TOKEN = "8d6fa7af6e054717b62674120f84cf43";

	private final static String APP_ID = "8a48b551506fd26f0150896d52ed481f";*/

	public static CCPRestSDK restAPI = null;
	
	static {
		initCCPRestSDK();
	}
	
	/**
	 * 初始化短信验证码SDK
	 * @return
	 */
	public static CCPRestSDK initCCPRestSDK(){
		restAPI = new CCPRestSDK();
		restAPI.init(SEND_URL, SEND_PORT);// 初始化服务器地址和端口，格式如下，服务器地址不需要写https://
		restAPI.setAccount(ACCOUNT_ID, AUTH_TOKEN);// 初始化主帐号和主帐号TOKEN
		restAPI.setAppId(APP_ID);// 初始化应用ID
		
		return restAPI;
	}
	
	/**
	 * 短信验证码
	 * @param mobile
	 * @param code
	 * @return
	 */
	public static boolean sendSms(String mobile, String code) {
		if(restAPI == null){
			initCCPRestSDK();
		}
		
		HashMap<String, Object> result = restAPI.sendTemplateSMS(mobile, SMS_CODE, new String[] { code, "10" });

		return "000000".equals(result.get("statusCode"));

	}
	public static void main(String[] agrs){
		System.out.println(sendSms("18520193363","123456"));
	}
	
	/**
	 * 语音验证码
	 * @param mobile
	 * @param code
	 * @return
	 */
	public static boolean sendVoiceVerify(String mobile, String code) {

		if(restAPI == null){
			initCCPRestSDK();
		}
		
		HashMap<String, Object> result = restAPI.voiceVerify(code, mobile, "4009920202", "3", "", "zh", "");

		return "000000".equals(result.get("statusCode"));
	}
	
	private static final String[] chars = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};

	/**
	 * 生成数字随机数
	 * @param length  需要生成随机数的长度
	 * @return 生成的随机数
	 */
	public static String generatorNumber(int length) {
		StringBuffer sb = new StringBuffer();
		Random r = new Random();
		for (int i = 0; i < length; i++) {
			sb.append(chars[r.nextInt(10)]);
		}
		return sb.toString();
	}
}
